<template>
    <div class="update-name">
        <van-nav-bar title="设置昵称" left-text="取消" right-text="完成" @click-left="$emit('colse')"
            @click-right="onConfirm" />
        <div class="field-wrap">
            <van-field v-model.trim="locaName" rows="2" autosize type="textarea" maxlength="10" placeholder="请输入昵称"
                show-word-limit />
        </div>
    </div>
</template>

<script>
import { updateUserprofile } from '@/api/user'
export default {
    name: 'UpdateName',
    mixins: [],
    components: {},
    props: {
        value: {
            type: String,
            required: true
        }
    },
    data() {
        return {
            locaName: this.value
        }
    },
    computed: {},
    watch: {},
    created() { },
    mounted() { },
    methods: {
        async onConfirm() {
            this.$toast.loading({
                message: '保存中...',
                forbidClick: true, // 禁止背景点击
                duration: 0 // 持续展示
            })
            try {
                const locaName = this.locaName
                if (locaName.length === 0) {
                    this.$toast('昵称不能为空')
                    return
                }
                await updateUserprofile({
                    name: locaName
                })
                // 更新视图
                this.$emit('input', locaName)
                // 关闭弹层
                this.$emit('colse')
                // 提示成功
                this.$toast.success('更新成功')
            } catch (err) {
                this.$toast.fail('更新失败')
            }
        }
    }
}
</script>

<style scoped lang="less">
.field-wrap {
    padding: 20px;
}
</style>